#include <rclcpp/rclcpp.hpp>
# include <turtle_bt_ros2/srv/spawn.hpp>

/*
目标：向服务器发送请求，生成一只新的乌龟
    话题：/spawn
    消息：turtlesim/Spawn
*/

int main(int argc, char **argv) {
    setlocale(LC_ALL, "");

    rclcpp::init(argc, argv);
    auto node = rclcpp::Node::make_shared("NPC_turtle");

    rclcpp::Client<turtle_bt_ros2::srv::Spawn>::SharedPtr client =
                    node->create_client<turtle_bt_ros2::srv::Spawn>("spawn");

    auto spawn_request = std::make_shared<turtle_bt_ros2::srv::Spawn::Request>();
    spawn_request->x = 0;
    spawn_request->y = 0;
    spawn_request->theta = 1.57;
    spawn_request->name = "turtle2";

    while (!client->wait_for_service()) {
        if (!rclcpp::ok()) {
            RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service. Exiting.");
            return 0;
        }
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "service not available, waiting again...");
    }

    auto result = client->async_send_request(spawn_request);

    if (rclcpp::spin_until_future_complete(node, result) ==
            rclcpp::executor::FutureReturnCode::SUCCESS) {
        RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "My turtle is  %s", result.get()->name.c_str());
    } else {
        RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service spawn");
    }
    rclcpp::spin(node);
    // rclcpp::shutdown();
    return 0;
}